<template>
  <div>
    <el-table :data="tableData" style="width: 100%;height:100%;">
      <el-table-column
          :prop="index"
          :label="item"
          v-for="(item, index) in tableHeader"
          :key="index"
      >
      </el-table-column>
      <el-table-column label="操作" fixed="right">
        <template #default="scope">
          <div v-if="scope.row.isEdit">
            <el-button type="primary" @click="handleRowConfirm(scope.row)">确定</el-button>
            <el-button type="danger" @click="handleRowCancel">取消</el-button>
          </div>
          <div v-else>
            <el-button type="primary" @click="handleRowEdit(scope.row)"> 编辑</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination align='center'
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="currentPage" :page-sizes="[1,5,10,20]"
                     :page-size="pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="numtotal">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {onMounted, reactive, toRefs} from "vue";
import {getModelPage10, getModelsColume, getModels, deleteModels} from "@/api/model";
import Vue from 'vue'
import { useRouter } from 'vue-router'

export default {
  name: "test",
  methods: {
    add() {
      this.$router.push("/manage/form?modelname="+this.modelname);
    },
    handleRowEdit(row) {
      // 编辑操作
      console.log('编辑', row);
      this.$router.push("/manage/form_edit?modelname="+this.modelname+"&id="+row["id"]);
    },
    handleRowDelete(row) {
      deleteModels(this.modelname,row.id).then(res => {
        // 删除操作
        console.log('删除成功');
        this.$router.push("/manage/table?modelname="+this.modelname);
        // 这里可以添加确认对话框
        //this.tableData.splice(index, 1); // 删除操作
      })

    },
    loaddata() {
      this.tableData = []
      var startIndex = ((this.currentPage - 1) * this.pageSize);
      const params = {
        order_column:'id',
        desc:'desc',
        userid:this.userid,
        start:startIndex,
        end:this.pageSize
      }
      getModels(this.modelname,params).then(res => {
        for(let b=0;b<res.data.data.length;b++){
          this.tableData.push(res.data.data[b]);
        }
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.loaddata();
    }
  },
  data() {
    return {
      currentPage: 1, // 当前页码
      total: 20, // 总条数
      pageSize: 10, // 每页的数据条数
    };
  },
  setup() {
    const router = useRouter();
    const modelname = router.currentRoute.value.query.modelname;
    var userid = ""
    if("userid" in router.currentRoute.value.query){
      userid = localStorage.getItem('UserId')
    }
    const state = reactive({
      tableHeader: {},
      tableData: [],
      numtotal: 0
    })
    // 调取api获取数据
    const getTableName = () => {
      getModelsColume(modelname).then(res => {
        for(let i=0;i<res.data.length;i++){
          var obj = res.data[i];
          state.tableHeader[obj.Field] = obj.Comment;
        }
      })
      const params = {
        size:10,
        order_column:'id',
        desc:'desc',
        userid:userid
      }
      getModelPage10(modelname,params).then(res => {
        state.numtotal = res.data.num
        for(let m=0;m<res.data.data.length;m++){
          state.tableData.push(res.data.data[m])
        }
      })
    }
    onMounted(getTableName)
    // ...toRefs()将state里面得对象解构
    return {
      ...toRefs(state),
      modelname,
      userid
    };
  }
}
</script>
